package com.dpj.test;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.WaitDemo")
public class WaitDemo {
    final static Object obj=new Object();
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
           synchronized (obj){
                log.debug("执行");
               try {
                   obj.wait();//让线程在obj上一直等下去
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
                log.debug("其他代码");
           }
        },"t1").start();

        new Thread(()->{
            synchronized (obj){
                log.debug("执行");
                try {
                    obj.wait();//让线程在obj上一直等下去
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其他代码");
            }
        },"t2").start();

        Thread.sleep(2000);
        log.debug("唤醒obj上其它线程");
        synchronized (obj){
            obj.notify();//唤醒obj上一个线程
         //   obj.notifyAll();//唤醒obj上所有等待线程
        }
    }

}
